namespace Sitecore.ChangesTracker.UI
{
  using System;
  using System.Globalization;
  using System.Web;
  using System.Web.UI;
  using Sitecore;
  using Data.Items;
  using Diagnostics;
  using Globalization;
  using Resources;
  using Shell.Applications.WebEdit;
  using Shell.Framework.Commands;
  using Shell.Web.UI.WebControls;
  using Sitecore.Web.UI.HtmlControls;
  using Text;
  using Web;
  using Web.UI.Sheer;
  using Web.UI.WebControls.Ribbons;

  
  public class PreviewDatePanel : RibbonPanel
  {
    
    public DateTime Current
    {
      get
      {
        return Sitecore.Web.WebEditUtil.GetCurrentDate();
      }

      set
      {
        Sitecore.Web.WebEditUtil.SetCurrentDate(value);
      }
    }

    public override void Render(HtmlTextWriter output, Ribbon ribbon, Item button, CommandContext context)
    {
      Assert.ArgumentNotNull(output, "output");
      Assert.ArgumentNotNull(ribbon, "ribbon");
      Assert.ArgumentNotNull(button, "button");
      Assert.ArgumentNotNull(context, "context");
      Render(output);
     
    }

    private static void Render(HtmlTextWriter output )
    {
      Assert.ArgumentNotNull(output, "output");
      CultureInfo culture = Context.User.Profile.Culture;
      output.Write("<table id=\"scPreviewDatePanel\" class=\"scRibbonToolbarDatePanel\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
      RenderRow(output, culture);
      output.Write("</table>");
    }

    private static void RenderRow(HtmlTextWriter output, CultureInfo culture)
    {
      output.Write("<tr style=\"height: 10px\">");
      RenderSelector(output, WebUtil.GetSessionDateTime("cht_sdate", DateTime.Today), culture, "start");
      RenderSelector(output, WebUtil.GetSessionDateTime("cht_edate", DateTime.Now), culture, "end");
      output.Write("</tr>");
    }

    private static void RenderSelector(HtmlTextWriter output, DateTime date, CultureInfo culture, string postfix)
    {
      output.Write("<td>");
      RenderPreviousButton(output, postfix);
      output.Write("</td>");
      output.Write("<td>");
      output.Write("<a href=\"#\" title=\"Change " +postfix+" date and time.\" class=\"scRibbonToolbarDateMonthSelector\" onclick=\"javascript:return scForm.invoke('restore:SelectDate" + postfix + "')\">");
      output.Write("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
      output.Write("<td rowspan=\"2\">");
      RenderDay(output, date, culture);
      output.Write("</td>");
      output.Write("<td rowspan=\"2\">");
      RenderDayOfWeek(output, date, culture);
      output.Write("</td>");
      output.Write("<td class=\"scRibbonToolbarDateMonthCell\">");
      RenderMonth(output, date, culture);
      output.Write("</td>");
      output.Write("</tr><tr>");
      output.Write("<td class=\"scRibbonToolbarDateYearCell\">");
      RenderYear(output, date, culture);
     
      output.Write("</td>");
      output.Write("</tr>");
      output.Write("<tr>");
      RenderTime(output, date, culture);
      output.Write("</tr>");
      output.Write("</table>");
      output.Write("</a>");
      
      output.Write("</td>");
      output.Write("<td rowspan=\"2\">");
      RenderNextButton(output, postfix);
      output.Write("</td>");
    }

    private static void RenderDay(HtmlTextWriter output, DateTime date, CultureInfo cultureInfo)
    {
      Assert.ArgumentNotNull(output, "output");
      Assert.ArgumentNotNull(cultureInfo, "cultureInfo");
      output.Write("<div class=\"\">");//scRibbonToolbarDateDay
      output.Write(date.Day);
      output.Write("</div>");
    }

    private static void RenderDayOfWeek(HtmlTextWriter output, DateTime date, CultureInfo cultureInfo)
    {
      Assert.ArgumentNotNull(output, "output");
      Assert.ArgumentNotNull(cultureInfo, "cultureInfo");
      output.Write("<div class=\"scRibbonToolbarDateDayOfWeek\">");
      output.Write(date.ToString("dddd", cultureInfo));
      output.Write("</div>");
    }

    private static void RenderMonth(HtmlTextWriter output, DateTime date, CultureInfo cultureInfo)
    {
      Assert.ArgumentNotNull(output, "output");
      Assert.ArgumentNotNull(cultureInfo, "cultureInfo");
      output.Write("<div class=\"\">");//scRibbonToolbarDateMonth
      output.Write(date.ToString("MMMM", cultureInfo));
      output.Write("</div>");
    }

    private static void RenderNextButton(HtmlTextWriter output,string postfix)
    {
      Assert.ArgumentNotNull(output, "output");
      output.Write("<a href=\"#\" class=\"scRibbonToolbarDateNext\" onclick=\"javascript:return scForm.invoke('restore:next"+postfix+"')\" >");
      ImageBuilder builder = new ImageBuilder();
      builder.Src = "Applications/32x32/navigate_right.png";
      builder.Width = 16;
      builder.Height = 16;
      builder.Alt = Translate.Text("Next Day");
     // builder.Class = "scRibbonToolbarDateNextIcon";
      output.Write(builder.ToString());
      output.Write("</a>");
    }

    private static void RenderPreviousButton(HtmlTextWriter output, string postfix)
    {
      Assert.ArgumentNotNull(output, "output");
      output.Write("<a href=\"#\" class=\"scRibbonToolbarDatePrevious\" onclick=\"javascript:return scForm.invoke('restore:prev"+postfix+"')\">");
      ImageBuilder builder = new ImageBuilder();
      builder.Src = "Applications/32x32/navigate_left.png";
      builder.Width = 16;
      builder.Height = 16;
      builder.Alt = Translate.Text("Previous Day");
      //builder.Class = "scRibbonToolbarDatePreviousIcon";
      output.Write(builder.ToString());
      output.Write("</a>");
    }

    private static void RenderTime(HtmlTextWriter output, DateTime date, CultureInfo cultureInfo)
    {
      Assert.ArgumentNotNull(output, "output");
      Assert.ArgumentNotNull(cultureInfo, "cultureInfo");
      bool flag = !string.IsNullOrEmpty(cultureInfo.DateTimeFormat.PMDesignator) || !string.IsNullOrEmpty(cultureInfo.DateTimeFormat.AMDesignator);
      output.Write("<div class=\"\">");//scRibbonToolbarDateTime
      if (flag)
      {
        output.Write(date.ToString("hh", cultureInfo));
      }
      else
      {
        output.Write(date.ToString("HH", cultureInfo));
      }

      output.Write(cultureInfo.DateTimeFormat.TimeSeparator);
      output.Write(date.ToString("mm", cultureInfo));
      if (flag)
      {
        output.Write("<span class=\"scRibbonToolbarDateTimeDesignator\">");//scRibbonToolbarDateTimeDesignator
        output.Write(date.ToString("tt", cultureInfo));
        output.Write("</span>");
      }

      output.Write("</div>");
    }

    private static void RenderYear(HtmlTextWriter output, DateTime date, CultureInfo cultureInfo)
    {
      Assert.ArgumentNotNull(output, "output");
      Assert.ArgumentNotNull(cultureInfo, "cultureInfo");
      output.Write("<div class=\"\">"); //scRibbonToolbarDateYear
      output.Write(date.ToString("yyyy", cultureInfo));
      output.Write("</div>");
    }

    private void Update()
    {
      SheerResponse.Eval("window.top.location='" + new UrlString(HttpUtility.UrlDecode(WebUtil.GetQueryString("url"))) + "'");
    }
  }
}

